﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Windows.Forms;
// 使用OleDb连接数据库
using System.Data.OleDb;

namespace 实验7_学生成绩管理系统
{
    class MyUtil
    {
        string myConnString = "Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=master";
        OleDbConnection conn = null;
        OleDbCommand cmd = null;
        DataTable dt = null;
        DataView dv = null;
        BindingSource bs = null;

        public void updateDataGridView(DataGridView dgv, BindingNavigator bn, string sql)
        {
            conn = new OleDbConnection();
            conn.ConnectionString = myConnString;
            cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = sql;

            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = cmd;

            dt = new DataTable();
            adapter.Fill(dt);

            bs = new BindingSource();
            dv = dt.DefaultView;
            bs.DataSource = dv;
            dgv.DataSource = bs;
            bn.BindingSource = bs;
        }
    }
}
